package main

import (
	"errors"
	"fmt"
	"log"
	"os"
	"strconv"

	"github.com/urfave/cli/v2"
)

func main() {
	app := &cli.App{
		Name:  "calculator-cli",
		Usage: "演示计算器的加减乘除",
		Commands: []*cli.Command{
			{
				Name:    "num",
				Aliases: []string{"n"},
				Usage:   "计算器数字",
				Flags: []cli.Flag{
					&cli.StringFlag{
						Name:    "type",
						Aliases: []string{"t"},
						Value:   "+",
						Usage:   "算法类型 (+, -, *,/)",
					},
				},
				Action: func(cCtx *cli.Context) error {
					calculateType := cCtx.String("type")
					if cCtx.Args().Len() == 0 {
						return errors.New("请输出参数")
					}
					if cCtx.Args().Len() != 2 {
						return errors.New("仅支持双位运算")
					}
					var first, laster int
					first, _ = strconv.Atoi(cCtx.Args().Get(0))
					laster, _ = strconv.Atoi(cCtx.Args().Get(1))
					switch calculateType {
					case "+":
						initial := first + laster
						fmt.Println("加法运算最终值", initial)
					case "-":
						initial := first - laster
						fmt.Println("减法运算最终值", initial)
					case "*":
						initial := first * laster
						fmt.Println("乘法法运算最终值", initial)
					default:
						initial := first / laster
						fmt.Println("除法运算最终值", initial)
					}
					return nil
				},
			},
		},
	}

	if err := app.Run(os.Args); err != nil {
		log.Fatal(err)
	}
}
